package com.eleadmin.order.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.ApplyCustomsFeeBiz;
import com.eleadmin.order.entity.ApplyCustomsFee;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2023-05-06 10:59:18
 * @version ：1.0
 */
@Repository
public class ApplyCustomsFeeBizImpl extends BaseDao implements ApplyCustomsFeeBiz {

    @Override
    public JsonResult list(PageResult2<T> page, ApplyCustomsFee applyCustomsFee, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<ApplyCustomsFee> rm = new BeanPropertyRowMapper<ApplyCustomsFee>(ApplyCustomsFee.class);
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.apply_id");
        sql.append(",a.type,a.fee_name,a.price,a.fee_modules,a.fee_modules_order");
		sql.append(",a.invoicename,a.invoicestate,a.currency,a.exchange_rate");
		sql.append(",a.remark,a.create_time,a.create_user,a.update_time");
		sql.append(",a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_apply_customs_fee a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
        sql.append(" where a.deleted=0 and a.apply_id=:apply_id");
        map.put("apply_id", applyCustomsFee.getApplyId());
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(applyCustomsFee)){
    //			if(Utils.isNotEmpty(applyCustomsFee.getName())){
    //				sql.append(" and instr(a.name,:name)>0 ");
    //				map.put("name", applyCustomsFee.getName());
    //			}
            if(Utils.isNotEmpty(applyCustomsFee.getSort())&&Utils.isNotEmpty(applyCustomsFee.getOrder())){
                sql.append(" order by a."+applyCustomsFee.getSort()+" "+applyCustomsFee.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<ApplyCustomsFee> list(ApplyCustomsFee applyCustomsFee, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<ApplyCustomsFee> rm = new BeanPropertyRowMapper<ApplyCustomsFee>(ApplyCustomsFee.class);
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.apply_id");
        sql.append(",a.type,a.fee_name,a.price,a.fee_modules,a.fee_modules_order");
		sql.append(",a.invoicename,a.invoicestate,a.currency,a.exchange_rate");
		sql.append(",a.remark,a.create_time,a.create_user,a.update_time");
		sql.append(",a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_apply_customs_fee a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
        sql.append(" where a.deleted=0 and a.apply_id=:apply_id");
        map.put("apply_id", applyCustomsFee.getApplyId());
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(applyCustomsFee)){
            if(Utils.isNotEmpty(applyCustomsFee.getSort())&&Utils.isNotEmpty(applyCustomsFee.getOrder())){
                sql.append(" order by a."+applyCustomsFee.getSort()+" "+applyCustomsFee.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public ApplyCustomsFee getById(String id) {
        RowMapper<ApplyCustomsFee> rm = new BeanPropertyRowMapper<ApplyCustomsFee>(ApplyCustomsFee.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.apply_id");
        sql.append(",a.type,a.fee_name,a.price,a.fee_modules,a.fee_modules_order");
		sql.append(",a.invoicename,a.invoicestate,a.currency,a.exchange_rate");
		sql.append(",a.remark,a.create_time,a.create_user,a.update_time");
		sql.append(",a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_apply_customs_fee a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
        sql.append(" where a.deleted=0 and a.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    public String getOrder(String feeModulesOrder) {
        if(feeModulesOrder.equals("运费")) { return "100"; }
        if(feeModulesOrder.equals("提箱")) { return "200"; }
        if(feeModulesOrder.equals("进港")) { return "300"; }
        if(feeModulesOrder.equals("其他")) { return "400"; }
        if(feeModulesOrder.equals("单项")) { return "500"; }
        if(feeModulesOrder.equals("采购")) { return "600"; }
        if(feeModulesOrder.equals("报关费")) { return "700"; }
        if(feeModulesOrder.equals("易港通")) { return "800"; }
        if(feeModulesOrder.equals("财务报销")) { return "900"; }
        return "1";
    }

    @Override
    public boolean save(ApplyCustomsFee applyCustomsFee, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into chf_apply_customs_fee (");
        sql.append("organization_id,apply_id,type,fee_name");
		sql.append(",price,fee_modules,fee_modules_order,invoicename,invoicestate");
		sql.append(",currency,exchange_rate,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:apply_id,:type,:fee_name");
		sql.append(",:price,:fee_modules,:fee_modules_order,:invoicename,:invoicestate");
		sql.append(",:currency,:exchange_rate,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("apply_id", applyCustomsFee.getApplyId());
        map.put("type", applyCustomsFee.getType());
        map.put("fee_name", applyCustomsFee.getFeeName());
        map.put("price", applyCustomsFee.getPrice());
        map.put("fee_modules", applyCustomsFee.getFeeModules());
        map.put("fee_modules_order", getOrder(applyCustomsFee.getFeeModules()));
        map.put("invoicename", applyCustomsFee.getInvoicename());
        map.put("invoicestate", applyCustomsFee.getInvoicestate());
        map.put("currency", applyCustomsFee.getCurrency());
        map.put("exchange_rate", applyCustomsFee.getExchangeRate());
        map.put("remark", applyCustomsFee.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(ApplyCustomsFee applyCustomsFee, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update chf_apply_customs_fee set ");
        sql.append("apply_id=:apply_id,type=:type,fee_name=:fee_name ");
		sql.append(",price=:price,fee_modules=:fee_modules,fee_modules_order=:fee_modules_order,invoicename=:invoicename ");
		sql.append(",invoicestate=:invoicestate,currency=:currency,exchange_rate=:exchange_rate ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("apply_id", applyCustomsFee.getApplyId());
        map.put("type", applyCustomsFee.getType());
        map.put("fee_name", applyCustomsFee.getFeeName());
        map.put("price", applyCustomsFee.getPrice());
        map.put("fee_modules", applyCustomsFee.getFeeModules());
        map.put("fee_modules_order", getOrder(applyCustomsFee.getFeeModules()));
        map.put("invoicename", applyCustomsFee.getInvoicename());
        map.put("invoicestate", applyCustomsFee.getInvoicestate());
        map.put("currency", applyCustomsFee.getCurrency());
        map.put("exchange_rate", applyCustomsFee.getExchangeRate());
        map.put("remark", applyCustomsFee.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", applyCustomsFee.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(id.length()>0){
            for(String id1 : id.split(",")){
                map  = new HashMap<String, String>();
                if(id1==null || id1.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update chf_apply_customs_fee set deleted=1" +
                    ",update_user=:update_user, update_time=now() "
                    + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}
